package hw;

import sk.Util;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

public class SavePanel extends JPanel implements ActionListener {
    private ATM atm;
    private JButton sureBtn;
    private JButton clearBtn;
    private Icon sureImg = Util.getImageIcon("/images/buttonSure.jpg");
    private Icon clearImg = Util.getImageIcon("/images/buttonOff.jpg");
    private JLabel save = new JLabel("请输入存款金额：");
    private JTextField savemoney = new JTextField();

    public SavePanel(ATM atm) {
        this.atm = atm;
        this.setLayout(null);
        this.setOpaque(false);

        save.setBounds(200, 200, 200, 30);
        save.setFont(new Font("黑体", Font.BOLD, 16));
        this.add(save);
        savemoney.setBounds(420, 200, 200, 30);
        this.add(savemoney);

        sureBtn = new JButton(sureImg);
        sureBtn.setBounds(200, 240, 140, 50);
        this.add(sureBtn);
        sureBtn.addActionListener(this);

        clearBtn = new JButton(clearImg);
        clearBtn.setBounds(420, 240, 140, 50);
        this.add(clearBtn);
        clearBtn.addActionListener(this);

    }

    public JTextField getSavemoney() {
        return savemoney;
    }

    public void setSavemoney(JTextField savemoney) {
        this.savemoney = savemoney;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == sureBtn) {
            JOptionPane.showMessageDialog(this, "存款成功，本次存入" + Double.valueOf(savemoney.getText()) + "元，您的余额为:" + (Double.valueOf(savemoney.getText()) + atm.getUsers().get(atm.getNum()).getMoney()));
            atm.getUsers().get(atm.getNum()).setMoney(Double.valueOf(savemoney.getText()) + atm.getUsers().get(atm.getNum()).getMoney());
            FileUtil.writeObject("d:\\user.txt", atm.getUsers());
            atm.switchPanel("menu");
        } else if (e.getSource() == clearBtn) {
            atm.switchPanel("menu");
        }
    }
}
